package cn.com.agree.pdfutilserver.web.controller;

import cn.com.agree.pdfutilserver.util.PdfUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HttpInController {
    // http://127.0.0.1:8080/savePdfToImage?pdfpath=lisi
    @RequestMapping("/savePdfToImage")
    @ResponseBody
    public String savePdfToImage(@RequestParam(name="pdfpath") String pdfpath,
                                 @RequestParam(name="imgPath") String imgPath,
                                 @RequestParam(name="imgtype") String imgtype,
                                 @RequestParam(name="coverPage") String coverPage,
                                 @RequestParam(name="dpi") String dpi) {
        boolean result = PdfUtils.savePageToPdf(coverPage,pdfpath,Integer.parseInt(dpi),imgPath,imgtype);
        return result?"success":"fail,please check log" ;
    }

    @RequestMapping("/openHTMLToPDF")
    @ResponseBody
    public String openHTMLToPDF(@RequestParam(name="htmlpath") String htmlpath,
                                 @RequestParam(name="pdfpath") String pdfpath) {
        boolean result = false;
        try{
            PdfUtils.htmlToPdf(htmlpath,pdfpath);
            result=true;
        }catch (Exception e){
            System.err.println(e);
            result=false;
        }
        return result?"success":"fail,please check log" ;
    }
}
